﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;
#endif
public static class ResourcesHelper
{
    public static GameObject InstantiatePrefabFromResourceSetDefault(string path,Transform parent=null)
    {
        if (parent == null)
        {
            parent = UIRoot.instance.GetUIRootCanvas().transform;
        }
        var prefab = Resources.Load<GameObject>(path);
        GameObject go = GameObject.Instantiate(prefab);
        go.transform.SetRectTransformStretchAllWithParent(parent);
        return go;
    }
    public static GameObject InstantiatePrefabFromABSetDefault(GameObject goAB, Transform parent= null)
    {
        if (parent==null)
        {
            parent = UIRoot.instance.GetUIRootCanvas().transform;
        }
        GameObject go = GameObject.Instantiate(goAB);
        go.transform.SetRectTransformStretchAllWithParent(parent);
        return go;
    }

    public static GameObject InstantiatePrefabFromABSetDefaultNotUI(GameObject goAB, Transform parent)
    {
        GameObject go = GameObject.Instantiate(goAB);
        go.transform.SetTransformDefalutWithParentNotUI(parent);
        return go;
    }
    public static GameObject CreateUI(string UIName)
    {
        GameObject go = ABManager.GetAsset<GameObject>(UIName);
        return ResourcesHelper.InstantiatePrefabFromABSetDefault(go);
    }

    public static GameObject CreateUIFromResource(string UIName)
    {
        GameObject go = Resources.Load<GameObject>(UIName);
        return ResourcesHelper.InstantiatePrefabFromABSetDefault(go);
    }
    public static GameObject CreateUIToCanvasTop(string UIName)
    {
        GameObject go = ABManager.GetAsset<GameObject>(UIName);
        var parent = UIRoot.instance.GetUIRootCanvasTop().transform;
        return ResourcesHelper.InstantiatePrefabFromABSetDefault(go, parent);
    }
    public static void RemoveUI(string UIName)
    {
        UIName = $"{UIName}(Clone)";
        var uiParentRoot1 = UIRoot.instance.GetUIRootCanvas().transform;
        var uiParentRoot2 = UIRoot.instance.GetUIRootCanvasTop().transform;
        Transform uiTrans1 = uiParentRoot1.Find(UIName);
        Transform uiTrans2 = uiParentRoot2.Find(UIName);
        if (uiTrans1 != null)
        {
            GameObject.Destroy(uiTrans1.gameObject);
            return;
        }
        if (uiTrans2 != null)
        {
            GameObject.Destroy(uiTrans2.gameObject);
            return;
        }
    }
    public static UnityEngine.Object Load(string path)
    {
        return Resources.Load(path);
    }

    public static string[] GetDependencies(string assetBundleName)
    {
        string[] dependencies = new string[0];
        if (!StaticData.isUseAssetBundle)
        {
#if UNITY_EDITOR
            dependencies = AssetDatabase.GetAssetBundleDependencies(assetBundleName, true);
#endif
        }
        else
        {
            dependencies = ResourcesComponent.AssetBundleManifestObject.GetAllDependencies(assetBundleName);
        }
        return dependencies;
    }

    public static string[] GetSortedDependencies(string assetBundleName)
    {
        Dictionary<string, int> info = new Dictionary<string, int>();
        List<string> parents = new List<string>();
        CollectDependencies(parents, assetBundleName, info);
        string[] ss = info.OrderBy(x => x.Value).Select(x => x.Key).ToArray();
        return ss;
    }

    public static void CollectDependencies(List<string> parents, string assetBundleName, Dictionary<string, int> info)
    {
        parents.Add(assetBundleName);
        string[] deps = GetDependencies(assetBundleName);
        foreach (string parent in parents)
        {
            if (!info.ContainsKey(parent))
            {
                info[parent] = 0;
            }
            info[parent] += deps.Length;
        }


        foreach (string dep in deps)
        {
            if (parents.Contains(dep))
            {
                throw new Exception($"包有循环依赖，请重新标记: {assetBundleName} {dep}");
            }
            CollectDependencies(parents, dep, info);
        }
        parents.RemoveAt(parents.Count - 1);
    }
}
